package net.melon.logger.inst.ssh;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.hibernate.type.UUIDCharType;

import net.esj.basic.dao.jdbc.JdbcDao;
import net.esj.basic.dao.jdbc.impl.JdbcDaoImpl;
import net.esj.basic.pojo.op.Operator;
import net.esj.basic.utils.DateFormat;
import net.esj.basic.utils.Pagination;
import net.esj.basic.utils.StringUtils;
import net.esj.basic.utils.UUIDCreator;
import net.melon.jdbc.core.RowMapper;
import net.melon.logger.manage.LoggerManage;
import net.melon.logger.manage.LoggerService;
import net.melon.logger.pojo.Logger;
import net.melon.logger.pojo.LoggerMethod;

@Singleton
@Named
public class LoggerServiceImpl implements
		LoggerService {

	JdbcDao jdbcDao;
	
	LoggerManage loggerManage;
	
	private static final String INSERT_METHOD = "INSERT INTO CORE_LOGGER_METHOD (ID,ACTION_NAME," +
			"METHOD_NAME,RESULT_NAME,ARGUMENTS,MESSAGE,OUTPUT_TYPE,FILE,LEVEL) VALUES (?,?,?,?,?,?,?,?,?)";
	@Override
	public void saveMethod(LoggerMethod method) {
		method.setId(UUIDCreator.create());
		jdbcDao.getJdbcTemplate().update(INSERT_METHOD, new Object[]{
				method.getId(),method.getActionName(),method.getMethodName(),method.getResultName(),method.getArguments(),
				method.getMessage(),method.getOutputType(),method.getFile(),method.getLevel()
		}, new int[]{Types.CHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
				Types.VARCHAR,Types.VARCHAR,Types.VARCHAR});
		loggerManage.reloadMethods();
	}

	private static final String UPDATE_METHOD = "UPDATE CORE_LOGGER_METHOD SET ACTION_NAME = ? ,METHOD_NAME = ?," +
			"RESULT_NAME = ? ,ARGUMENTS= ?,MESSAGE=?,OUTPUT_TYPE= ?,FILE= ?,LEVEL = ? WHERE ID = ?";
	@Override
	public void updateMethod(LoggerMethod method) {
		jdbcDao.getJdbcTemplate().update(UPDATE_METHOD, new Object[]{
				method.getActionName(),method.getMethodName(),method.getResultName(),method.getArguments(),
				method.getMessage(),method.getOutputType(),method.getFile(),method.getLevel(),method.getId()
		}, new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
				Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.CHAR});
		loggerManage.reloadMethods();
	}

	private static final String DELETE_METHOD = "DELETE FROM  CORE_LOGGER_METHOD WHERE ID = ?";
	
	@Override
	public void deleteMethod(String id) {
		jdbcDao.getJdbcTemplate().update(DELETE_METHOD, new Object[]{id});
		loggerManage.reloadMethods();
	}

	private static final String PAGE_METHOD = "SELECT * FROM CORE_LOGGER_METHOD ";
	@Override
	public Pagination<LoggerMethod> pageMethod(
			Pagination<LoggerMethod> pagination) {
		pagination = jdbcDao.queryPage(PAGE_METHOD, new RowMapper<LoggerMethod>() {

			@Override
			public LoggerMethod mapRow(ResultSet set, int arg1)
					throws SQLException {
				LoggerMethod method = new LoggerMethod();
				method.setArguments(set.getString("ARGUMENTS"));
				method.setActionName(set.getString("ACTION_NAME"));
				method.setResultName(set.getString("RESULT_NAME"));
				method.setFile(set.getString("FILE"));
				method.setId(set.getString("ID"));
				method.setLevel(set.getString("LEVEL"));
				method.setMessage(set.getString("MESSAGE"));
				method.setMethodName(set.getString("METHOD_NAME"));
				method.setOutputType(set.getString("OUTPUT_TYPE"));
				return method;
			}
		}, pagination, "order by id desc");
		return pagination;
	}
	
	private static final String LOAD_METHOD = "SELECT * FROM CORE_LOGGER_METHOD WHERE ID = ? ";
	@Override
	public LoggerMethod loadMethod(String id) {
		LoggerMethod method = jdbcDao.getJdbcTemplate().queryForObject(LOAD_METHOD, new RowMapper<LoggerMethod>() {

			@Override
			public LoggerMethod mapRow(ResultSet set, int arg1)
					throws SQLException {
				LoggerMethod method = new LoggerMethod();
				method.setArguments(set.getString("ARGUMENTS"));
				method.setActionName(set.getString("ACTION_NAME"));
				method.setResultName(set.getString("RESULT_NAME"));
				method.setFile(set.getString("FILE"));
				method.setId(set.getString("ID"));
				method.setLevel(set.getString("LEVEL"));
				method.setMessage(set.getString("MESSAGE"));
				method.setMethodName(set.getString("METHOD_NAME"));
				method.setOutputType(set.getString("OUTPUT_TYPE"));
				return method;
			}
		}, new Object[]{id});
		return method;
	}

	private static final String PAGE_LOGGER = "SELECT * FROM CORE_LOGGER WHERE 1=1 ";
	@Override
	public Pagination<Logger> pageLogger(Pagination<Logger> pagination,String operatorName,LoggerMethod method,String keywords,Date startDate,Date endDate) {
		StringBuilder sb= new StringBuilder(PAGE_LOGGER);
		if(StringUtils.hasText(operatorName)){
			sb.append("AND OPERATOR_NAME = '").append(operatorName).append("' ");
		}
		if(method!=null && StringUtils.hasText(method.getId())){
			sb.append(" AND METHOD_ID = '").append(method.getId()).append("' ");
		}
		if(StringUtils.hasText(keywords)){
			sb.append(" AND MESSAGE LIKE '%").append(keywords).append("%' ");
		}
		if(startDate!=null){
			sb.append(" AND GMT_CREATE >= '").append(DateFormat.formateDateTime(startDate, DateFormat.DEFAULT_DATETIME_FORMAT_SEC)).append("' ");
		}
		if(endDate!=null){
			sb.append(" AND GMT_CREATE <= '").append(DateFormat.formateDateTime(endDate, DateFormat.DEFAULT_DATETIME_FORMAT_SEC)).append("' ");
		}
		pagination = jdbcDao.queryPage(sb.toString(), new RowMapper<Logger>(){

			@Override
			public Logger mapRow(ResultSet rs, int rowNum) throws SQLException {
				Logger logger = new Logger();
				logger.setGmtCreate(rs.getTimestamp("GMT_CREATE"));
				logger.setId(rs.getString("ID"));
				logger.setMessage(rs.getString("MESSAGE"));
				logger.setMethodId(rs.getString("METHOD_ID"));
				logger.setOperatorId(rs.getString("OPERATOR_ID"));
				logger.setOperatorName(rs.getString("OPERATOR_NAME"));
				logger.setOperatorType(rs.getString("OPERATOR_TYPE"));
				return logger;
			}
			
		}, pagination, "order by id desc");
		return pagination;
	}
	
	@Override
	public Pagination<Logger> pageLogger(Pagination<Logger> pagination) {
		StringBuilder sb= new StringBuilder(PAGE_LOGGER);
		pagination = jdbcDao.queryPage(sb.toString(), new RowMapper<Logger>(){

			@Override
			public Logger mapRow(ResultSet rs, int rowNum) throws SQLException {
				Logger logger = new Logger();
				logger.setGmtCreate(rs.getTimestamp("GMT_CREATE"));
				logger.setId(rs.getString("ID"));
				logger.setMessage(rs.getString("MESSAGE"));
				logger.setMethodId(rs.getString("METHOD_ID"));
				logger.setOperatorId(rs.getString("OPERATOR_ID"));
				logger.setOperatorName(rs.getString("OPERATOR_NAME"));
				logger.setOperatorType(rs.getString("OPERATOR_TYPE"));
				return logger;
			}
			
		}, pagination, "order by gmtCreate desc");
		return pagination;
	}
	
	@Override
	public Logger saveLogger(Logger logger){
		loggerManage.saveLoggerToDB(logger);
		return logger;
	}
	@Inject
	public void setLoggerManage(LoggerManage loggerManage) {
		this.loggerManage = loggerManage;
	}

	@Inject
	public void setJdbcDao(JdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}



	
}
